package com.eagle.view.fragment.menu;

import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eagle.R;
import com.eagle.support.helper.CallHelper;
import com.eagle.view.activity.base.BaseCallActivity;
import com.kinstalk.voip.sdk.logic.sip.SipLogicApi;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.ObjectAnimator;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

/**
 * Created by gc on 2017/1/6.
 */
@EFragment(R.layout.layout_menu_dialog)
public class FragmentMenu extends Fragment {

    public static final int FROM_LIVE_SHOW_MASTER = 0;

    public static final int FROM_LIVE_SHOW_AUDIENCE = 4;

    public static final int FROM_VIDEO_CALL = 1;

    public static final int FROM_MEETING = 2;

    @ViewById(R.id.view_cover)
    protected View mViewCover;

    @ViewById(R.id.layout_dialog)
    protected LinearLayout mLayoutDialog;

    @ViewById(R.id.tv_mute)
    protected TextView mTvMute;

    @ViewById(R.id.tv_message)
    protected TextView mTvMessage;

    @ViewById(R.id.tv_switch_sound_source)
    protected TextView mTvHandset;

    @ViewById(R.id.tv_hang_up)
    protected TextView mTvHangup;

    @ViewById(R.id.tv_invite)
    protected TextView mTvInvite;

    @ViewById(R.id.tv_hide_camera)
    protected TextView mTvHideCamera;

    @ViewById(R.id.tv_switch_camera)
    protected TextView mTvSwitchCamera;

    @ViewById(R.id.tv_connect_video)
    protected TextView mTvConnectVideo;

    private int mHeight;

    //听筒是否打开
    private boolean mHandsetOn;

    //预览是否打开, 默认打开
    private boolean mPreviewOn = true;

    //是否连麦
    private boolean mVideoConnected;

    private BaseCallActivity mActivity;

    private CallHelper mCallHelper;

    private boolean mCoverClicked;

    @AfterViews
    void initView() {
        mActivity = (BaseCallActivity) getActivity();
        mCallHelper = mActivity.getCallHelper();
        mHeight = getMeasureHeight(mLayoutDialog);
    }

    public void showDialog(int from) {
        mLayoutDialog.setVisibility(View.VISIBLE);
        mTvConnectVideo.setVisibility(from == FROM_LIVE_SHOW_AUDIENCE ? View.VISIBLE : View.GONE);
        mTvMessage.setVisibility((from == FROM_LIVE_SHOW_MASTER || from == FROM_LIVE_SHOW_AUDIENCE) ? View.VISIBLE : View.GONE);
        mTvInvite.setVisibility(from == FROM_MEETING ? View.VISIBLE : View.GONE);
        mTvHideCamera.setVisibility(from == FROM_MEETING ? View.GONE : View.VISIBLE);
        mTvHangup.setVisibility(from == FROM_MEETING ? View.GONE : View.VISIBLE);
        mTvMute.setVisibility(from == FROM_MEETING ? View.GONE : View.VISIBLE);
        mTvSwitchCamera.setVisibility(from == FROM_MEETING ? View.GONE : View.VISIBLE);
        mViewCover.setOnTouchListener((view, motionEvent) -> {
            if (!mCoverClicked) {
                mCoverClicked = true;
                hideDialog(null);
            }
            return true;
        });
        ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(mViewCover, "alpha", 0.0f, 1.0f);
        animatorAlpha.setDuration(300).start();
        ObjectAnimator.ofFloat(mLayoutDialog, "translationY", mHeight, 0).setDuration(300).start();
    }

    public boolean isShowing() {
        return mLayoutDialog.getVisibility() == View.VISIBLE;
    }

    @Click({R.id.tv_hang_up, R.id.tv_cancel, R.id.tv_message})
    public void hideDialog(View view) {
        ObjectAnimator animatorAlpha = ObjectAnimator.ofFloat(mViewCover, "alpha", 1.0f, 0.0f);
        animatorAlpha.setDuration(300).start();
        ObjectAnimator.ofFloat(mLayoutDialog, "translationY", 0, mHeight).setDuration(300).start();
        animatorAlpha.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mCoverClicked = false;
                mLayoutDialog.setVisibility(View.GONE);
                mViewCover.setOnTouchListener(null);
                if (view != null) {
                    if (view.getId() == R.id.tv_hang_up) {
                        mActivity.popQuitLiveDialog();
                    } else if (view.getId() == R.id.tv_message) {
                        mActivity.handlerMessageInput(true);
                    }
                }
                mActivity.getSupportFragmentManager().popBackStack();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
    }

    @Click(R.id.tv_mute)
    protected void soundOff() {
        if (mCallHelper.getLoader().getCurrentCallInfo() != null) {
            if (mCallHelper.isSoundOff()) {
                mCallHelper.getLoader().getCurrentCallInfo().setTransmitingAudioMute(false);
                mTvMute.setText("静音");
            } else {
                mCallHelper.getLoader().getCurrentCallInfo().setTransmitingAudioMute(true);
                mTvMute.setText("取消静音");
            }
        }
        hideDialog(null);
    }

    /**
     * 切换听筒
     */
    @Click(R.id.tv_switch_sound_source)
    protected void switchSound() {
        mHandsetOn = !mHandsetOn;
        SipLogicApi.setHandfree(null, mHandsetOn);
        if (!mHandsetOn) {
            mTvHandset.setText("切换听筒");
        } else {
            mTvHandset.setText("切换外放");
        }
        hideDialog(null);
    }

    /**
     * 禁用开启预览
     */
    @Click(R.id.tv_hide_camera)
    protected void hideCamera() {
        mPreviewOn = !mPreviewOn;
        if (!mPreviewOn) {
            mActivity.hideCamera(true);
            mTvHideCamera.setText("开启预览");
        } else {
            mActivity.hideCamera(false);
            mTvHideCamera.setText("禁用预览");
        }
        hideDialog(null);
    }

    /**
     * 切换摄像头
     */
    @Click(R.id.tv_switch_camera)
    protected void switchCamera() {
        mActivity.switchCamera();
        hideDialog(null);
    }

    /**
     * 连麦/取消连麦
     */
    @Click(R.id.tv_connect_video)
    protected void connectVideo() {
        if (mVideoConnected) {
            mCallHelper.leaveConnectBySlaver();
            mTvConnectVideo.setText("连麦");
        } else {
            mCallHelper.joinLiveBySlaver();
            mTvConnectVideo.setText("取消连麦");
        }
        mVideoConnected = !mVideoConnected;
        mActivity.refreshConnectState(mVideoConnected);
        hideDialog(null);
    }

    private int getMeasureHeight(View view) {
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        return view.getMeasuredHeight();
    }

    public void hideDialog() {
        hideDialog(null);
    }
}
